In [1]:
import matplotlib.pylab as plt
import seaborn as sns
import numpy as np

sns.set(palette="hls", font_scale=1.5)

Magic Command

  • %matplotlib inline: show() 생략
  • %matplotlib qt: 외부창 출력

In [2]:
# make point with cumulative sum
points = np.random.randn(50).cumsum()
points


Out[2]:
array([-0.44903035,  0.88850127,  2.82617058,  3.19364287,  3.67317435,
        4.97244028,  5.21490344,  5.51114539,  6.25558292,  6.31076217,
        5.66292099,  3.94162038,  3.55234961,  4.41565842,  3.495583  ,
        1.36153566,  1.08091599,  0.81608762,  2.89139916,  2.64366258,
        2.31362626,  1.37015303,  1.51544357,  0.98647411,  0.36378188,
        2.52982007,  2.74491586,  3.25135377,  1.77700468,  0.39754323,
        0.25938772, -1.56122388, -1.73898918, -4.16859636, -2.53041761,
       -2.95016077, -3.3648134 , -3.87431721, -3.73396515, -3.51815724,
       -3.85365039, -4.53475989, -4.42975443, -3.94471572, -3.67839213,
       -5.31563986, -5.42195834, -6.84146472, -6.6465074 , -6.25595458])

Line plot


In [3]:
# plt.plot(x, y): x, y = point(x, y) on coordinate
# put y only(default x = auto)
plt.plot(points)
plt.show()



In [4]:
# put x and y points
plt.plot(range(0, 250, 5), points)
plt.show()



In [5]:
# set color, marker, line
plt.plot(points, 'co:')
plt.show()



In [6]:
# style setting
plt.plot(points, 'co-', lw=3, ms=5, mfc='b') # lw=linewidth, ms=marker size, mfc=marker face color
plt.xlim(-10, 60) # set x axis limit
plt.ylim(-5, 5) # set y axis limit
plt.show()



In [7]:
# style setting
plt.plot(points, 'co-', lw=3, ms=5, mfc='b')
plt.xlim(-10, 60) # set x axis limit
plt.ylim(-5, 5) # set y axis limit
plt.xticks([0, 25, 50]) # set x axis ticks
plt.yticks([-7, -3, 1], [r'$\theta$', r'2$\theta$', r'3$\theta$']) # LaTeX input available
plt.grid(False) # grid off
plt.show()



In [8]:
# draw multiple lines
## plt.plot(x1, y1, xy1_style, x2, y2, xy2_style, x3, y3, xy3_style)
plt.plot(points, points, 'bo',
         points, 2*points, 'cs-',
         points, 0.5*points, 'r.', lw=0.5, ms=8)
plt.show()



In [9]:
# draw multiple lines -2
plt.plot(points, 'co-', lw=3, ms=5, mfc='b')
plt.plot(points*0.5)

plt.show()


Legend, Title

  • plt.legend(loc=x): x = legend location
  • plt.xlabel("label name"): set x label as "label name"
  • plt.ylabel("label name"): set y label as "label name"
  • plt.title("plot title"): set plot title as "plot title")

In [10]:
# legend, title
plt.rc('font', family='nanumgothic') # set font family, use Korean
plt.plot(points, label='random points') # set plot 1 label
plt.plot(0.5 * points, label='임의값') # set plot 2 label
plt.legend()
plt.xlabel('random x') # set x label
plt.ylabel('random y') # set y label
plt.title('random plot') # set the title
plt.show()



In [11]:
plt.plot(points)
plt.annotate(# text, arrow point(x, y), xy coordinate
             r'(text)', xy=(40, -4), xycoords='data',
             # text location from text coordinate, text coordinate
             xytext=(-50, 50), textcoords='offset points',
             # font, arrow shape
             fontsize=20, arrowprops=dict(arrowstyle="->", linewidth=3, color="b"))
plt.show()


Figure size

  • plt.figure(figsize=(x, y)): set the figure size as x, y

In [12]:
plt.figure(figsize=(20, 3))
plt.plot(points)
plt.show()


Axes, Subplots


In [13]:
ax1 = plt.subplot(2, 1, 1)
plt.plot(points)

ax2 = plt.subplot(2, 1, 2)
plt.plot(np.random.randn(50))

plt.show()



In [14]:
x = [3, 2, 1]
y = [1, 2, 3]
xlabel = ['한개', '두개', '세개']

# plt.bar: vertical / plt.barh: horizontal
plt.bar(x, y, align='center') # align: ceter(default), edge
plt.xticks(x, xlabel)
plt.show()



In [15]:
x = np.random.randint(0, 10, 10)
print(x)

arrays, bins, patches = plt.hist(x, bins=6)
plt.show()


[0 4 8 3 2 3 4 4 4 0]

In [16]:
# value counts for each bin
print(arrays)

# the range of each bin
print(bins)


[ 2.  1.  2.  4.  0.  1.]
[ 0.          1.33333333  2.66666667  4.          5.33333333  6.66666667
  8.        ]

In [17]:
plt.pie([30, 50, 10], # size
        labels = ['피자', '햄버거', '감자튀김'], # label
        colors = ['pink', 'salmon', 'tomato'], # colors
        explode = (0.01, 0.01, 0.2), # explode
        autopct = '%.2f%%', # set the ratio label format
        shadow = True, # pie chart shadow
        startangle = 0) # rotate the chart
plt.axis('equal') # chart shape slope
plt.title('품목별 매출비중')
plt.show()